<?php

namespace app\common\model;

use basic\ModelBasic;

class CommonDetail extends ModelBasic
{
	use \traits\ModelTrait;

	/**
	 * 关联类型可用值
	 * @var [type]
	 */
	public static $rel_types = [
		'route' => '线路',
		'mall'  => '商城'
	];

	/**
	 * 添加详情
	 * @Author   FW
	 * @DateTime 2024-03-27
	 * @Email    123763712@qq.com
	 * @param    [type]           $rel_type 关联类型：route、mall
	 * @param    [type]           $rel_id   关联ID
	 * @param    [type]           $details  [description]
	 */
	public static function addDetails($rel_type , $rel_id, $details)
	{
		if ( !in_array($rel_type , array_keys(static::$rel_types)) ) {
			throw new \Exception('rel_type值错误，可用' . join(',' , array_keys(static::$rel_types)));
			return false;
		}

		return self::saveOne([
            'rel_type'=> $rel_type,
            'rel_id'  => $rel_id,
            'details' => html_to_entities($details)
        ]);
	}

	/**
	 * 获取详情
	 * @Author   FW
	 * @DateTime 2024-03-27
	 * @Email    123763712@qq.com
	 * @param    [type]           $rel_type   关联类型：route、mall
	 * @param    [type]           $rel_id     [description]
	 * @param    boolean          $for_mobile [description]
	 * @return   [type]                       [description]
	 */
	public static function getDetails($rel_type , $rel_id , $for_mobile = true)
	{
		$details = self::where('rel_type',$rel_type)->where('rel_id',$rel_id)->value('details');

		if ( !empty($details) ) {
			$details = entities_to_html($details);

			if ( $for_mobile ) {
				$details = preg_replace('/<img/', '<img style="max-width:690rpx;height:auto;" ', $details);
				$details = preg_replace('/<video/', '<video style="max-width:690rpx;height:auto;" ', $details);
			}
		}

		return $details;
	}
}//Class End
